const checkData = require("../utils/checkData")
const { addInfo,getInfo } = require("../controller/sessionsController")
const { successModel, errorModel, lackModel, timeCheck } = require("../utils/resModel")
const sessionsRouter = async (req, res) => {
   /**
     * 
     * @api {POST} api/sessions/addInfo 新增场次信息
     * @apiName 新增场次信息
     * @apiGroup Session
     * @apiVersion  1.0.0
     * 
     * 
     * @apiParam  {String} startTime 开始时间
     * @apiParam  {String} endTime 结束时间
     * @apiParam  {Number} allNum 总人数
     * @apiParam  {Number} timeId 时间ID
     * @apiParam  {String} date 预约日期
     * 
     * 
     * @apiParamExample  {type} 参数:
     * {
     *     startTime : "12:02",
     *     endTime : "14:32",
     *     allNum : 66,
     *     timeId : 19,
     *     date : "2020/11/26"
     * }
     * 
     * @apiSuccessExample {type} 成功返回:
     * {
     *  "msg" : 成功添加场次
     * }
     * 
     */
    if (req.method === "POST" && req.path === "/api/sessions/addInfo") {
        let checkRes = checkData(req, "startTime", "endTime", "allNumer", "timeId", "date");
        
        if (typeof checkRes === "boolean") {
            req.body.startTimeStamp = new Date(`${req.body.date} ${req.body.startTime}`).getTime();
            req.body.endTimeStamp = new Date(`${req.body.date} ${req.body.endTime}`).getTime();

            if (req.body.endTimeStamp > req.body.startTimeStamp) {
                let sessionsRes = await addInfo(req.body);
                if (sessionsRes.affectedRows) {
                    return successModel("成功添加场次")
                } else {
                    return errorModel(sessionsRes.message);
                }
            } else {
                return errorModel("结束时间必须大于开始使时间");
            }
        } else {
            return lackModel(`${checkRes}参数格式不正确`);
        }
    }

     /**
     * 
     * @api {GET} api/sessions/getInfo 获取场次信息
     * @apiName 获取场次信息
     * @apiGroup   Session
     * @apiVersion  1.0.0
     * 
     * @apiParam  {Number} timeId 时间ID
     * 
     * @apiParamExample  {type} 参数:
     * {
     *     timeId : 16,
     * }
     * 
     * @apiSuccessExample {type} 成功返回:
     * [
            {
            end_time: "1606372320000"
            id: 16
            is_appointment: 1
            start_time: "1606363320000"
            }
     * ]
     */
    if (req.method === "GET" && req.path === "/api/sessions/getInfo") {
        let checkRes = checkData(req, "timeId");
        if (typeof checkRes === "boolean") {
            let sessionsRes = await getInfo(req.query);
            
                return successModel (sessionsRes);
           
        } else {
            return lackModel(`${checkRes}参数格式不正确`);
        }
    }


/**
     * 
     * @api {PUT} api/appointment/updateInfo 修改预约信息
     * @apiName 修改预约信息
     * @apiGroup appointment
     * @apiVersion  1.0.0
     * 
     * 
     * @apiParam  {String} token token值
     * 
     * 
     * @apiParamExample  {type} Request-Example:
     * {
     *     token:"c356034e43e65119b5b937654cedfe7a1604916889014"
     * }
     * 
     * 
     * @apiSuccessExample {type} Success-Response:
     * {
     *     msg: "修改成功";
     * }
     * 
     * 
     */
    if (req.method === "PUT" && req.path === "/api/sessions/updateInfo") {
        return {
            code: 200,
            msg: "成功修改场次信息",
            data: [{}, {}, {}]
        }
    }
}
module.exports = sessionsRouter